package com.zhima.annotation;

import com.config.Knife4jConfig;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

/**
 * @author : 芝麻
 * @date : 2023-09-01 09:59
 **/
public class ZhimaKnife4jImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
        Map<String, Object> enableKnife4jAttributes = annotationMetadata.getAnnotationAttributes(EnableKnife4j.class.getName());
        String title = (String) enableKnife4jAttributes.getOrDefault("title", "芝麻OJ");
        String description = (String) enableKnife4jAttributes.getOrDefault("description", "芝麻OJ");
        String basePackage = (String) enableKnife4jAttributes.getOrDefault("basePackage", "com.zhima.controller");
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
        rootBeanDefinition.setBeanClass(Knife4jConfig.class);
        ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
        constructorArgumentValues.addIndexedArgumentValue(0, title);
        constructorArgumentValues.addIndexedArgumentValue(1, description);
        constructorArgumentValues.addIndexedArgumentValue(2, basePackage);
        rootBeanDefinition.setConstructorArgumentValues(constructorArgumentValues);
        registry.registerBeanDefinition("knife4jConfig", rootBeanDefinition);
    }
}
